package com.woniu.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.*;
import com.woniu.mapper.*;
import com.woniu.service.FlowerOrderService;
import com.woniu.vo.CartVoCZT;
import com.woniu.vo.OrderItemVoCZT;
import com.woniu.vo.OrderVoCZT;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class FlowerOrderServiceImpl implements FlowerOrderService {
    @Resource
    private FlowerOrderMapper fom;
    @Resource
    private FlowerOrderItemMapper foim;
    @Resource
    private UserAddressMapper uam;
    @Resource
    private FlowerCartMapper fcm;
    @Resource
    private FlowerInfoMapper fim;
    @Resource
    private Snowflake snowflake;

    @Override
    public Long insertFlowerOrderFromCart(int accountId, int[] cartIds) {
        FlowerOrder order = new FlowerOrder();
        BigDecimal total = new BigDecimal(0);
        //设置订单id
        order.setOrderId(snowflake.nextId());
        //设置用户id
        order.setAccountId(accountId);
        //查询默认地址
        Example addressExample = new Example(UserAddress.class);
        Example.Criteria criteria = addressExample.createCriteria();
        criteria.andEqualTo("accountId", accountId);
        criteria.andEqualTo("isDefault", (byte) 1);
        UserAddress userAddress = uam.selectOneByExample(addressExample);
        if (userAddress==null) {
            return 1L;
        }
        order.setAddressId(userAddress.getAddressId());

        int i = 0;
        for (int cartId : cartIds) {
            CartVoCZT cartVoCZT = fcm.selectCartByCartId(cartId);
            //新增详情
            FlowerOrderItem flowerOrderItem = new FlowerOrderItem();
            flowerOrderItem.setOrderId(order.getOrderId());
            flowerOrderItem.setFlowerId(cartVoCZT.getFlowerId());
            flowerOrderItem.setBuyCount(cartVoCZT.getBuyCount());
            BigDecimal sum = cartVoCZT.getFlowerPrice().multiply(new BigDecimal(cartVoCZT.getBuyCount()));
            flowerOrderItem.setSubTotal(sum);
            flowerOrderItem.setCreateTime(new Date());
            i = foim.insert(flowerOrderItem);
            if (i == 0) {
                break;
            }
            //减去库存数量并增加售出数量
            Example example = new Example(FlowerInfo.class);
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.andEqualTo("flowerId", cartVoCZT.getFlowerId());
            FlowerInfo flowerInfo = fim.selectOneByExample(example);
            FlowerInfo temp = new FlowerInfo();
            temp.setFlowerId(cartVoCZT.getFlowerId());
            temp.setFlowerStoreCount(flowerInfo.getFlowerStoreCount() - cartVoCZT.getBuyCount());
            temp.setFlowerSaleCount(flowerInfo.getFlowerSaleCount() + cartVoCZT.getBuyCount());
            i = fim.updateByPrimaryKeySelective(temp);
            if (i == 0) {
                break;
            }
            //购物车数据逻辑删除
            FlowerCart flowerCart = new FlowerCart();
            flowerCart.setCartId(cartId);
            flowerCart.setIsDelete((byte) 1);
            i = fcm.updateByPrimaryKeySelective(flowerCart);
            if (i == 0) {
                break;
            }
            total = total.add(sum);
        }
        //设置总价
        order.setTotal(total);
        //设置新增时间
        order.setCreateTime(new Date());
        //设置订单状态
        order.setStatus((byte) 1);
        int insert = fom.insert(order);
        if (i == 1 && insert == 1) {
            return order.getOrderId();
        } else {
            return 0L;
        }
    }

    @Override
    public Long insertFlowerOrderFromFlowerInfo(int accountId, int buyCount, int flowerId, BigDecimal flowerPrice) {
        FlowerOrder order = new FlowerOrder();

        //设置订单id
        order.setOrderId(snowflake.nextId());
        //设置用户id
        order.setAccountId(accountId);
        //查询默认地址
        Example addressExample = new Example(UserAddress.class);
        Example.Criteria criteria = addressExample.createCriteria();
        criteria.andEqualTo("accountId", accountId);
        criteria.andEqualTo("isDefault", (byte) 1);
        UserAddress userAddress = uam.selectOneByExample(addressExample);
        if (userAddress==null) {
            return 1L;
        }
        order.setAddressId(userAddress.getAddressId());
        //新增详情
        FlowerOrderItem flowerOrderItem = new FlowerOrderItem();
        flowerOrderItem.setOrderId(order.getOrderId());
        flowerOrderItem.setFlowerId(flowerId);
        flowerOrderItem.setBuyCount(buyCount);
        BigDecimal total = flowerPrice.multiply(new BigDecimal(buyCount));
        flowerOrderItem.setSubTotal(total);
        flowerOrderItem.setCreateTime(new Date());
        foim.insert(flowerOrderItem);
        //减去库存数量并增加售出数量
        Example example = new Example(FlowerInfo.class);
        Example.Criteria criteria1 = example.createCriteria();
        criteria1.andEqualTo("flowerId", flowerId);
        FlowerInfo flowerInfo = fim.selectOneByExample(example);
        FlowerInfo temp = new FlowerInfo();
        temp.setFlowerId(flowerId);
        temp.setFlowerStoreCount(flowerInfo.getFlowerStoreCount() - buyCount);
        temp.setFlowerSaleCount(flowerInfo.getFlowerSaleCount() + buyCount);
        fim.updateByPrimaryKeySelective(temp);
        //设置总价
        order.setTotal(total);
        //设置新增时间
        order.setCreateTime(new Date());
        //设置订单状态
        order.setStatus((byte) 1);
        int insert = fom.insert(order);
        if (insert == 1) {
            return order.getOrderId();
        } else {
            return 0L;
        }
    }

    @Override
    public OrderVoCZT selectOrderByOrderId(String orderId) {
        return fom.selectOrderByOrderId(orderId);
    }

    @Override
    public int updateAddressIdByOrderId(String orderId, int addressId) {
        FlowerOrder order = new FlowerOrder();
        order.setOrderId(Long.valueOf(orderId));
        order.setAddressId(addressId);
        return fom.updateByPrimaryKeySelective(order);
    }

    @Override
    public Integer updateOrderByOrderId(FlowerOrder order) {
        return fom.updateByPrimaryKeySelective(order);
    }

    @Override
    public PageInfo<OrderVoCZT> selectAllOrderByAccountId(int accountId,
                                                          String orderId,
                                                          byte isComment,
                                                          String startTime,
                                                          String endTime,
                                                          int status,
                                                          int pageNum,
                                                          int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderVoCZT> list = fom.selectAllOrderByAccountId(accountId, orderId, isComment, startTime, endTime, status);
        return new PageInfo<>(list);
    }

    @Override
    public int deleteOrderByOrderId(String orderId) {
        int i = 0;
        OrderVoCZT vo = fom.selectOrderByOrderId(orderId);
        for (OrderItemVoCZT orderItem : vo.getOrderItems()) {
            int flowerId = orderItem.getFlowerId();
            FlowerInfo flowerInfo = fim.selectByPrimaryKey(flowerId);
            flowerInfo.setFlowerStoreCount(flowerInfo.getFlowerStoreCount() + orderItem.getBuyCount());
            flowerInfo.setFlowerSaleCount(flowerInfo.getFlowerSaleCount() - orderItem.getBuyCount());
            i = fim.updateByPrimaryKey(flowerInfo);
            if (i == 0) {
                break;
            }
        }
        if (i == 1) {
            FlowerOrder order = new FlowerOrder();
            order.setOrderId(Long.valueOf(orderId));
            order.setStatus((byte) 0);
            i = fom.updateByPrimaryKeySelective(order);
        }
        return i;
    }

}
